/**
 * Ref https://blog.csdn.net/chase_hung/article/details/82284788
 */

// Qt
#include <QLabel>
#include <QPushButton>
#include <QQmlContext>
#include <QQuickItem>
#include <QQuickWidget>

// my
#include "MyWindow.h"

MyWindow::MyWindow(QWidget* parent)
    : QWidget(parent)
    , m_pQuickTitle(nullptr)
    , m_pQuickRect(nullptr)
    , m_pQuickListView(nullptr)
    , m_pQuickItem(nullptr)
    , m_pBtnResize(nullptr)
    , m_Model()
    , m_zoomIn(false)
    , m_top(60) {

    // this->setWindowFlags(Qt::FramelessWindowHint);
    // this->setAttribute(Qt::WA_TranslucentBackground);
    this->resize(800, 500);

    // add widget
    build();
}
//---------------------------------
MyWindow::~MyWindow() {
}
//---------------------------------
int MyWindow::actionDialog(QString type) {
    qDebug() << "MyWindow::actionDialog(" << type << ") ";
    // return 0;
    if (type == "Rect") {
        addQuickWidget();
    }
    else if (type == "cModel") {
        addWidgetListView();
    }
    else {
        return 1101;
    }
    return 0;
}
//---------------------------------
void MyWindow::build() {

    // load qml
    int x = 5;
    int y = 10;

    m_pBtnResize = new QPushButton(this);
    m_pBtnResize->resize(60, 40);
    m_pBtnResize->move(x, y);
    m_pBtnResize->setText("resize");

    // title
    x = x + m_pBtnResize->width() + 5;
    y = 0;

    m_pQuickTitle = new QQuickWidget(this);
    m_pQuickTitle->rootContext()->setContextProperty("cp_MyDialog", this);

    // set sample model
    m_Model.sample(); // set sample data
    m_pQuickTitle->rootContext()->setContextProperty("$Model", &m_Model);
    m_pQuickTitle->setSource(QUrl("qrc:/MyTitle.qml"));
    m_pQuickTitle->setResizeMode(QQuickWidget::SizeRootObjectToView);
    m_pQuickTitle->setGeometry(x, y, width() - x, m_top);
    m_pQuickTitle->show();

    // after load qml get QuickItem
    QQuickItem* pQuickItem = m_pQuickTitle->rootObject();
    if (pQuickItem) {
        // set property of qml by C++ directly
        pQuickItem->setProperty("modelSource", 1);

        // connect qml signal to c++ slot
        connect(pQuickItem, SIGNAL(sendType(QString)), this, SLOT(actionDialog(QString)),
                Qt::QueuedConnection);
    }
}
//---------------------------------
void MyWindow::addQuickWidget() {
    if (m_pQuickRect) {
        delete m_pQuickRect;
        m_pQuickRect = nullptr;
        m_pQuickItem = nullptr;
        disconnect(m_pBtnResize, 0, 0, 0);
        return;
    }

    m_pQuickRect = new QQuickWidget(this);

    m_pQuickRect->setSource(QUrl("qrc:/MyWidget.qml"));
    m_pQuickRect->setResizeMode(QQuickWidget::SizeRootObjectToView);

    m_pQuickRect->setGeometry(0, m_top, width(), height() - m_top);
    m_pQuickRect->show();

    m_pQuickItem = m_pQuickRect->rootObject();
    m_pQuickItem->setProperty("displayWidth", 400);
    m_pQuickItem->setProperty("displayHeight", 300);

    connect(m_pBtnResize, &QPushButton::clicked, [ = ] {
        if (!m_pQuickItem) return;

        double ratio = m_zoomIn ? 0.8 : 0.4;
        m_zoomIn     = !m_zoomIn;
        m_pQuickItem->setProperty("displayWidth", 800 * ratio);
        m_pQuickItem->setProperty("displayHeight", 600 * ratio);
    });
}
//---------------------------------
void MyWindow::addWidgetListView() {
    if (m_pQuickListView) {
        delete m_pQuickListView;
        m_pQuickListView = nullptr;
        return;
    }
    m_pQuickListView = new QQuickWidget(this);

    m_pQuickListView->setSource(QUrl("qrc:/MyWidgetListView.qml"));
    m_pQuickListView->setResizeMode(QQuickWidget::SizeRootObjectToView);

    m_pQuickListView->setGeometry(0, m_top, width(), height() - m_top);
    m_pQuickListView->show();

    QQuickItem* pQuickItem = m_pQuickListView->rootObject();

    pQuickItem = nullptr;
}
//---------------------------------
void MyWindow::resizeEvent(QResizeEvent* event) {
    QWidget::resizeEvent(event);
    if (m_pQuickTitle) {
        m_pQuickTitle->resize(width() - m_pQuickTitle->x(), m_top);
    }
    if (m_pQuickListView) {
        m_pQuickListView->resize(width(), height() - m_top);
    }
    if (m_pQuickRect) {
        m_pQuickRect->resize(width(), height() - m_top);
    }
}
